home *** CD-ROM | disk | FTP | other *** search
/ PC-Blue - MS DOS Public Domain Library / PC-Blue MS-DOS Public Domain Library - NYACC.iso / vol086 / roff46.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-12-15  |  7.7 KB  |  286 lines

  1. /********************************************************/
  2. /*                            */
  3. /*            ROFF4, Version 1.60            */
  4. /*                            */
  5. /*(C) 1983,4 by Ernest E. Bergmann            */
  6. /*        Physics, Building #16            */
  7. /*        Lehigh Univerisity            */
  8. /*        Bethlehem, Pa. 18015            */
  9. /*                            */
  10. /* Permission is hereby granted for all commercial and    */
  11. /* non-commercial reproduction and distribution of this    */
  12. /* material provided this notice is included.        */
  13. /*                            */
  14. /********************************************************/
  15. /*Jan 14, 1984*/
  16.  
  17. /********************************************************/
  18. /* March 1984  DeSmet C version */
  19. /* Henry Harpending*/
  20. /* Anthropology Department*/
  21. /* University of New Mexico*/
  22. /* Albuquerque, NM  87131*/
  23. /*   no restrictions */
  24. /*********************************************************/
  25. #include "a:roff4.h"
  26. /**************************************************/
  27. int value(base,string)    /*unsigned conversion*/
  28. int base;        /*radix for conversion*/
  29. char *string;        /*no leading blanks please!*/
  30.             /*trailing whitespace or '\0'*/
  31. {int val,d;
  32. char c;
  33.     val=0;
  34.     for(d=digit(*string);d>=0 && d<base ; d=digit(*string))
  35.         {val = val*base + d;
  36.         string++;
  37.         }
  38.     c=*string;
  39.     if(!c || c==' ' || c==TAB || c=='\n') return(val);
  40.     else return(-1);    /*error return is -1*/
  41. }
  42. /**************************************************/
  43. int digit(d)
  44. char d;
  45. {    d=toupper(d);
  46.     if(d<='9') return(d-'0');
  47.     if(d<'A') return(-1); /*error return is negative val*/
  48.     if(d<='Z') return(10-'A'+d);
  49.     return(-1);    /*error*/
  50. }
  51. /**************************************************/
  52. strln3(s,word,num)
  53.  /*returns printed string length; checks legality of
  54.         word function;keeps track of vertical
  55.         excursions; records them in globals*/
  56. char *s;
  57. int word;    /* boolean, if true, check is made for none
  58.         black characters in the string*/
  59. int num;    /* for expansion of NUMSIGN;set 1 to ignore*/
  60. {int i,i2,p1,p2,p3;
  61. int t,b,h;    /*vertical vars*/
  62. char c, *ss;
  63. ss=s;
  64. t=b=h=0;
  65. p3=p2=p1=-LSZ;
  66. for(c=*s,i2=i=0;c;c=*(++s))
  67.     {if(c==NUMSIGN)
  68.         {i++;
  69.         if(num>9) i++;
  70.         if(num>99) i++;
  71.         if(num>999) i++;
  72.         }
  73.     else if((c!=TCVAL)&&(c!=CFVAL))
  74.         {if((c<=' ')&&(word)) goto error;
  75.         else i++;
  76.         }
  77.     else if(c==CFVAL)
  78.         {c=*(++s);
  79.         if(c==TCVAL) goto error;/*both CFVAL,TCVAL*/
  80.         switch(c)
  81.         {case 'h':
  82.         case 'H':if(i>i2) i2=i;
  83.              if(i) i--;
  84.             else goto error;/*before start*/
  85.             break;
  86.         case '+': h--; if(h<t) t=h; break;
  87.         case '-': h++; if(h>b) b=h; break;
  88.         case 'B':
  89.         case 'b':
  90.         case 'D':
  91.         case 'd':
  92.         case 'u':
  93.         case 'U':
  94.         case 'X':
  95.         case 'x': break;
  96.         case '(': p1=i; break;
  97.         case '[': p2=i; break;
  98.         case '{': p3=i; break;
  99.         case ')': if(i>i2) i2=i; i=p1; break;
  100.         case ']': if(i>i2) i2=i; i=p2; break;
  101.         case '}': if(i>i2) i2=i; i=p3; break;
  102.         default: if(CPTR[c-' ']) break;
  103.         goto error;    /*undecipherable*/
  104.         }}
  105.     else/*c==TCVAL*/
  106.         {if(class(*(s+1))!=BLACK)
  107.         goto error;    /*illegal translation*/
  108.         }
  109.     }
  110. if(h) goto error;
  111. if(word){WTOP=t;
  112.     WBOT=b;
  113.     }
  114. else    {LTOP=t;
  115.     LBOT=b;
  116.     }
  117. if(i>=i2)return(i);
  118. /* else prints beyond last character: */
  119. error:
  120.     /*should be fprint -> stderr*/
  121.     fprintf(stderr,"STRLN3:<%s> is illegally formed\n",
  122.                 ss);
  123.     return(strlen(ss));
  124. }
  125. /* A properly formed token string has its first printable
  126. character indicating the lefthand edge and the last printable
  127. character at the right hand edge.  Only legal control pairs
  128. accepted.  It must consist of printable symbols. */
  129. /************************************
  130. set stack like set() sets a variable
  131. *************************************/
  132. setS(param,val,arg_typ,defval,minval,maxval)
  133. int param[STKSIZ+1],val,defval,minval,maxval;
  134. char arg_typ;
  135. {int i;
  136.     if(val==NO_VAL)
  137.         {for(i=0;i<STKSIZ;i++)    /*pop*/
  138.         param[i]=param[i+1];
  139.         param[STKSIZ]=defval;
  140.         }
  141.     else    {for(i=STKSIZ;i;i--)    /*push*/
  142.         param[i]=param[i-1];
  143.         if (arg_typ=='+') *param+=val;
  144.         else if (arg_typ=='-') *param-=val;
  145.         else *param=val;
  146.         }
  147.     *param=min(max(*param,minval),maxval);
  148. if DEBUG fprintf(stderr,"\n  setS: *param = %d",*param);
  149. }
  150. /******************************************
  151. initialize stack type variable, st, with v
  152. *******************************************/
  153. initsk(st,v)
  154. int st[STKSIZ+1],v;
  155. {int i;
  156.     for(i=STKSIZ+1;i;st[--i]=v);
  157. }
  158. /**************************************************/
  159. gettr()    /*process .tr */
  160. {char chr,*pchr,getcode();
  161. char wrdbuf[MAXLINE];
  162.     getwrd(LINE,wrdbuf);    /*remove .tr*/
  163.     if(getwrd(LINE,wrdbuf)==WE_HAVE_A_WORD)chr=*wrdbuf;
  164.     else return;    /*error: missing args*/
  165.  
  166.     pchr = TREND;
  167.     if('.'==getcode())
  168.         TPTR[chr-' ']=pchr;    /*record pointer*/
  169.     else    {TREND=pchr;
  170.     fprintf(stderr,"\nError for .TR; error in line:\n%s",
  171.         LINE);
  172.     }
  173. }
  174. /**************************************************/
  175. getpc()    /*process .pc, printer control */
  176. {char chr,*pchr,getcode();
  177. char wrdbuf[MAXLINE];
  178.     getwrd(LINE,wrdbuf);    /*remove .pc*/
  179.     if(getwrd(LINE,wrdbuf)==WE_HAVE_A_WORD)chr=*wrdbuf;
  180.     else return;    /*error: missing args*/
  181.  
  182.     pchr = TREND;
  183.     if('.'==getcode())
  184.         CPTR[chr-' ']=pchr;    /*record pointer*/
  185.     else    {TREND=pchr;
  186.     fprintf(stderr,"\nError for .PC; error in line:\n%s",
  187.         LINE);
  188.     }
  189. }
  190. /**************************************************/
  191. /*added start() and complete() to avoid contention on TRTBL*/
  192. char getcode()    /*LINE must contain the radix as the first
  193.         token and it and the following lines then
  194.         contain code values finally delimited by a
  195.         token that starts with a '.' ; comments can
  196.         be at the end of any of these lines, set off by
  197.         " ;" */
  198. {int base,code;    /*conversion radix, value*/
  199. char *pcode,ncode;
  200. char wrdbuf[MAXLINE];
  201.     if(TREND>(&TRTBL[TRSIZ-128]))
  202.         fprintf(stderr,"\nTR table full");
  203.     if(getwrd(LINE,wrdbuf)==WE_HAVE_A_WORD)
  204.        {switch(toupper(*wrdbuf))
  205.         {case 'B': base=2;break;
  206.         case 'O':
  207.         case 'Q': base=8;break;
  208.         case 'D': base=10;break;
  209.         case 'H': base=16;break;
  210.         default: return(FALSE);    /*error*/
  211.         }
  212.     if DEBUG
  213.     fprintf(stderr,"\nGETCODE:radix token=<%s>,base=<%d>",
  214.                     wrdbuf, base);
  215.        }
  216.     else return(FALSE);    /*error: missing arg*/
  217. start();
  218.     pcode =TREND++;
  219.     *pcode=ncode = 0;
  220.     while(ncode<127)
  221.     {while(getwrd(LINE,wrdbuf)!=WE_HAVE_A_WORD)
  222.         fgets2(LINE,ifp);
  223.     if DEBUG fprintf(stderr,"\nGETTR: next token is <%s>",
  224.                 wrdbuf);
  225.      if(';'==*wrdbuf) fgets2(LINE,ifp);/*comment*/
  226.      else if('.'==*wrdbuf)
  227.         {*pcode = ncode;        /*save #*/
  228.         complete();
  229.         return(*wrdbuf);
  230.         }
  231.      else    {
  232.         if((code=value(base,wrdbuf)) > -1)
  233.             {*(TREND++) = code;
  234.              ncode++ ;
  235.             }
  236.         else {complete();
  237.              return(*wrdbuf);    /*conversion error*/
  238.              }
  239.         }
  240.     }
  241.     complete();
  242.     fprintf(stderr,"\nGETCODE: code sequence too long");
  243.     return(FALSE);
  244. }
  245. /**************************************************/
  246. ocode()    /*process .ou*/
  247. {char wrdbuf[MAXLINE],*pcode,*p;
  248.     getwrd(LINE,wrdbuf);    /*remove .ou*/
  249.     p=pcode=TREND;
  250.     if('.'==getcode()) outstr(p);
  251.     else fprintf(stderr,"\nOCODE: error in:\n%s",LINE);
  252.     TREND=pcode;
  253. }
  254. /**************************************************/
  255. outstr(p)    /*print string whose bytecount is *p */
  256. char *p;
  257. {int i;
  258. for(i=*(p++); i; i--) cputc(*(p++),ofp);
  259. }
  260. /**************************************************/
  261. getfr()    /*process .FR ;cf. ocode() */
  262. {char *pchr,getcode(),wrdbuf[MAXLINE];
  263.     getwrd(LINE,wrdbuf);
  264.     if(getwrd(LINE,wrdbuf)==WE_HAVE_A_WORD)
  265.         FRVAL = atoi(wrdbuf);
  266.     else return;
  267.     FRVAL=max(1,FRVAL); FRVAL=min(FRVAL,4);
  268.     pchr=TREND;
  269.     if('.'==getcode()) FRSTRING=pchr;
  270.     else    {TREND = pchr;
  271.         fprintf(stderr,"\nError for .FR in:\n%s\n",
  272.             LINE);
  273.         }
  274. }
  275. /**************************************************/
  276. getwh()    /*process .WH ;cf. gettr() */
  277. {char *pcode, getcode(), wrdbuf[MAXLINE];
  278.     getwrd(LINE,wrdbuf);
  279.     pcode = TREND;
  280.     if('.'==getcode()) WHSTRING=pcode;
  281.     else    {TREND = pcode;
  282.         fprintf(stderr,"\nError for .WH in:\n%s\n",
  283.             LINE);
  284.         }
  285. }
  286.